<?php
namespace Admin\Controller;
use Admin\Common\CommonController; //使用并继承CommonController类，用作权限的授权
class UserController extends CommonController{
	//显示用户列表
	public function userListAction(){
		$sql = "select * from user";
		$data_m = M();
		$user_res = $data_m->query($sql);
		// var_dump($user_res);
		// exit();
		$this->assign('user_res',$user_res);
		$this->display();
	}

	//增加用户
	public function addUserAction(){
		if(IS_POST){
			// var_dump(I('post.'));
			$userName = I('post.user-name');
			$sex = I('post.sex');
			$userTel = I('post.user-tel');
			$userEmail = I('post.email');
			$userProvince = I('post.province');
			$userCity = I('post.city');
			$userComment = I('post.comment');
			$regist_time = date("Y-m-d H:i:s");
			$sql="insert into user(username,sex,phone,mail,province,city,comment,regist_time) values('$userName','$sex','$userTel','$userEmail','$userProvince','$userCity','$userComment','$regist_time')";
			$data_m = M();
			$res = $data_m->execute($sql);
			// var_dump($res);
			if($res){
				$this->success("插入用户成功",U("Admin/User/userSuccess"),2); //用一个页面关闭新窗口
				// $this->success("插入用户成功",U("Admin/User/userList"),2); //用异步做
			}else{
				$this->error("插入用户失败,请重新操作",U("Admin/User/addUser"),2); 
			}
		}else{
			$this->display();
		}
		
	}

	//编辑用户
	public function editUserAction(){
		// var_dump(I('POST.uid'));
		// exit();
		$uid=I("get.id");
		// var_dump($uid);
		if(IS_POST){ 			//有数据提交时
			$uid=I("POST.uid");
			$username=I("POST.user-name");
			$sex=I("POST.sex");
			$phone=I("POST.user-tel");
			$email=I("POST.email");
			$province=I("POST.province");
			$city=I("POST.city");
			$comment=I("POST.comment");
			$sql = "update user set username='$username',sex='$sex',phone='$phone',mail='$email',province='$province',city='$city',comment='$comment' where id='$uid'";
			$data_m=M();
			$res = $data_m->execute($sql);
			$data = array('success' => $res );
			echo json_encode($data);
		
		}else{		//没有数据提交时
			$sql = "select * from user where id='{$uid}'";
			$data_m=M();
			$res = $data_m->query($sql);
			// var_dump($res);
			// exit();
			$this->assign('res',current($res));
			$this->display();
		}
		

	}

	//密码修改
	public function userPasswordEditAction(){
		$uid=I('get.id');
		if(IS_POST){		
			$oldPassword = MD5(I('post.oldpassword'));
			$newPassword = MD5(I('post.newpassword'));
			$newPassword2 = MD5(I('post.newpassword2'));		
			$sql = "select password from user where id=$uid";
			$data_m=M();
			$res = $data_m->query($sql);
			$res = current($res);
			if($res['password']==$oldPassword){
				if($newPassword == $newPassword2){
					$sql = "update user set password = '$newPassword' ";
					$data_m = M();
					$res = $data_m->execute($sql);
					if($res){
						$this->success("密码修改成功",U("Admin/User/userSuccess"),2); 
					}else{
						$this->error("密码修改失败1",U("Admin/User/userPasswordEdit"),2); 
					}
				}

			}else{
				$this->error("密码修改失败2",U("Admin/User/userPasswordEdit"),2); 
			}
			
		}else{
			$this->display();
		}
		
	}

	//删除用户
	public function delUserAction()
	{	
		$uid = I('post.uid');
		$sql = "delete from user where id = '$uid'";
		$data_m = M();
		$res = $data_m->execute($sql);
		$data = array('success' => $res );
		echo json_encode($data);
	}

	//给一个新的方法让弹出的窗口关闭
	public function userSuccessAction()
	{
		$this->display();
	}

	//关闭用户账号
	public function userStopAction(){
		$uid = I("post.uid");
		$sql="update user set status=0 where id=$uid";
		$data_m=M();
		$result=$data_m->execute($sql);
		$data = array('success'=>$result);
		echo json_encode($data);
	}
	//开启用户
	public function userStartAction(){
		$uid = I("post.uid");
		$sql="update user set status=1 where id=$uid";
		$data_m=M();
		$result=$data_m->execute($sql);

		$data = array('success'=>$result);
		echo json_encode($data);
		// $this->display();
	}

	//用户查看
	public function userShowAction(){
		$uid=I('get.id');
		$sql="select * from user where id='$uid'";
		$data_m = M();
		$user_res = $data_m->query($sql);
		$user_res=current($user_res);
		$this->assign('user_res',$user_res);
		// var_dump($user_res);
		// exit();
		$this->display();
	}

	public function recordBrowseAction(){
		$this->display();
	}

	//用户职位管理
	public function positionListAction(){
		$sql="select * from position_t";
		$data_m=M();
		$position_res=$data_m->query($sql);
		// var_dump($position_res);
		// exit();
		$this->assign('position_res',$position_res);
		$this->display();
	}

	//用户职位添加
	public function addPositionAction(){
		if(IS_POST){
			$positionName=I('post.positionName');
			$sql="insert into position_t(pname) values('$positionName')";
			$data_m=M();
			$positionRes=$data_m->execute($sql);
			// var_dump($positionRes);
			// exit();
			if($positionRes){
				$this->success("添加职位成功，请重新刷新",U('Admin/Category/userSuccess'),2);
			}else{
				$this->error("添加话题失败",U('Admin/Category/themeList'),2);
			}
			
		}else{
			$this->assign('position_res',$position_res);
			$this->display();
		}
	}

	//编辑职位
	public function editPositionAction(){
		//当有POST传输数据时
		if(IS_POST){
			$positionId=I('post.positionId');
			$positionName=I('post.positionName');
			$sql="update position_t set pname='$positionName' where id='$positionId'";
			$data_m=M();
			$result=$data_m->execute($sql);
			// var_dump($result);
			// exit();
			$arr = array('success' =>$result);
			echo json_encode($arr);
			
		}elseif(IS_GET){	
		//当点击编辑时							
			$positionid=I('get.id');
			$sql="select * from position_t where id='$positionid'";
			$data_m=M();
			$position_res=$data_m->query($sql);
			$position_res=current($position_res);
			// var_dump($position_res);
			// exit();
			$this->assign("position_res",$position_res);
			$this->display();
		}
		
	}


	//用户职位删除
	public function delPositionAction(){
		// var_dump(I('post.positionid'));
		// exit();
		$positionid=I('post.positionid');
		$sql="delete from position_t where id='$positionid'";
		$data_m=M();
		$position_res=$data_m->execute($sql);
		$arr=array('success'=>$position_res);
		echo json_encode($arr);
	}


}

?>